package ds_industry_2025.industry.gy_08.T1

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

import java.text.SimpleDateFormat
import java.util.{Calendar, Properties}

// todo 数据抽取部分
object t1_count {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .master("local[*]")
      .appName("数据抽取")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .enableHiveSupport()
      .getOrCreate()

    val conn=new Properties()
    conn.setProperty("user","root")
    conn.setProperty("password","123456")
    conn.setProperty("driver","com.mysql.jdbc.Driver")

    val day=Calendar.getInstance()
    day.add(Calendar.DATE,-1)
    val yesterday=new SimpleDateFormat("yyyyMMdd").format(day.getTime)


    //  todo 定义抽取的方法01:不需要改变字段的
    def to_ods(mysql_name:String,ods_name:String):Unit={
      spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_industry?useSSL=false",mysql_name,conn)
        .withColumn("etldate",lit(yesterday))
        .write.format("hive").mode("append")
        .partitionBy("etldate")
        .saveAsTable(s"ods.${ods_name}")
      println(s"${ods_name}数据写入完成")
    }

    //  todo 定义抽取的方法02:需要剔除字段的
    def to_ods2(mysql_name: String, ods_name: String): Unit = {
      spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_industry?useSSL=false", mysql_name, conn)
        .drop("ProducePrgCode")
        .withColumn("etldate", lit(yesterday))
        .write.format("hive").mode("append")
        .partitionBy("etldate")
        .saveAsTable(s"ods.${ods_name}")
      println(s"${ods_name}数据写入完成")
    }

    to_ods("EnvironmentData","environmentdata")
    to_ods("ChangeRecord","changerecord")
    to_ods("BaseMachine","basemachine")
    to_ods2("ProduceRecord","producerecord")
    to_ods("MachineData","machinedata")




    spark.close()
  }

}
